package com.uzai.console.web.wxpub;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.locallive.tlbtoken.TlbTokenQuery;
import com.uzai.console.dto.sysdomain.SysDomainQuery;
import com.uzai.console.dto.tb.tbtoken.TbTokenQuery;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupQuery;
import com.uzai.console.dto.wechatprivate.configplan.*;
import com.uzai.console.entity.SysDomain;
import com.uzai.console.entity.UserTypeGroup;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.SysConfigMapper;
import com.uzai.console.mapper.SysDomainMapper;
import com.uzai.console.mapper.UserTypeGroupMapper;
import com.uzai.console.service.configplan.ConfigPlanService;
import com.uzai.console.vo.base.SelectIdVo;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.sysplugin.SysPluginVo;
import com.uzai.console.vo.takebal.redpacket.RedCardTempVo;
import com.uzai.console.vo.wechatprivate.configplan.ConfigPlanSelectListVo;
import com.uzai.console.vo.wechatprivate.configplan.ConfigPlanVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 公众号配置方案
 * @author cen
 * @date 2022-4-21 15:15:53
 */
@Api(value = "公众号配置方案", tags = "公众号|配置方案")
@ApiSort(102)
@RestController
public class WxpubConfigPlanController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(WxpubConfigPlanController.class);

    @Autowired
    private ConfigPlanService configPlanService;
    @Autowired
    private UserTypeGroupMapper userTypeGroupMapper;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private SysDomainMapper sysDomainMapper;
    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    @ApiOperation(value = "查询配置方案列表", notes = "查询配置方案列表", response = ConfigPlanSelectListVo.class)
    @ApiOperationSupport(order=1)
    @PostMapping("/wxpub/configPlan/findConfigPlanList")
    public Object findConfigPlanList(@RequestBody ConfigPlanQuery query){
        //延迟0.5秒，保证第一次进来，先执行查询默认客户端
        try {
            Thread.sleep(500);
        }catch (Exception e){

        }
        query.setDeviceType(DeviceTypeEnum.WX_PUB.getKey());
        return configPlanService.findConfigPlanSelectList(query);
    }

    @ApiOperation(value = "查询配置方案详情", notes = "查询配置方案详情", response = ConfigPlanVo.class)
    @ApiOperationSupport(order=2)
    @PostMapping("/wxpub/configPlan/findConfigPlanInfo")
    public Object findConfigPlanInfo(@RequestBody ConfigPlanIdDto configPlanIdDto){
        return configPlanService.findConfigPlanInfo(configPlanIdDto);
    }

    @SystemLogType(model = SystemModelEnum.DeviceWxpubConfigPlan, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改配置方案", notes = "配置方案修改")
    @ApiOperationSupport(order=3)
    @PostMapping("/wxpub/configPlan/updateConfigPlan")
    public Object updateConfigPlan(@RequestBody ConfigPlanUpdateDto configPlanUpdateDto){
        configPlanService.updateConfigPlan(configPlanUpdateDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWxpubConfigPlan, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除配置方案", notes = "配置方案删除")
    @ApiOperationSupport(order=4)
    @PostMapping("/wxpub/configPlan/deleteConfigPlan")
    public Object deleteConfigPlan(@RequestBody ConfigPlanIdDto configPlanIdDto){
        configPlanService.deleteConfigPlan(configPlanIdDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWxpubConfigPlan, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加配置方案", notes = "添加方案删除", response = ConfigPlanIdDto.class)
    @ApiOperationSupport(order=5)
    @PostMapping("/wxpub/configPlan/addConfigPlan")
    public Object addConfigPlan(@RequestBody ConfigPlanAddDto configPlanIdDto){
        configPlanIdDto.setDeviceType(DeviceTypeEnum.WX_PUB.getKey());
        Long id = configPlanService.addConfigPlan(configPlanIdDto);
        ConfigPlanIdDto ConfigPlanIdDto = new ConfigPlanIdDto();
        ConfigPlanIdDto.setId(id);
        return ConfigPlanIdDto;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWxpubConfigPlan, type = OperationTypeEnum.COPY)
    @ApiOperation(value = "复制配置方案", notes = "复制方案删除", response = ConfigPlanIdDto.class)
    @ApiOperationSupport(order=6)
    @PostMapping("/wxpub/configPlan/copyConfigPlan")
    public Object copyConfigPlan(@RequestBody ConfigPlanCopyDto configPlanCopyDto){
        Long id = configPlanService.copyConfigPlan(configPlanCopyDto);
        ConfigPlanIdDto ConfigPlanIdDto = new ConfigPlanIdDto();
        ConfigPlanIdDto.setId(id);
        return ConfigPlanIdDto;
    }

    @ApiOperation(value = "查询插件详情", notes = "查询插件详情", response = SysPluginVo.class)
    @ApiOperationSupport(order=7)
    @PostMapping("/wxpub/configPlan/findSysPluginInfo")
    public Object findSysPluginInfo(@RequestBody ConfigPlanPluginIdDto configPlanPluginIdDto){
        return configPlanService.findSysPluginInfo(configPlanPluginIdDto);
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatSysPlugin, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改插件信息", notes = "修改插件信息")
    @ApiOperationSupport(order=8)
    @PostMapping("/wxpub/configPlan/updateSysPlugin")
    public Object updateSysPlugin(@RequestBody ConfigPlanPluginUpdateDto configPlanPluginUpdateDto){
        Long id = configPlanService.updateSysPlugin(configPlanPluginUpdateDto);
        ConfigPlanPluginIdDto configPlanPluginIdDto = new ConfigPlanPluginIdDto();
        configPlanPluginIdDto.setId(id);
        return configPlanPluginIdDto;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatSysPlugin, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改配置方案中的联盟", notes = "修改配置方案中的联盟")
    @ApiOperationSupport(order=9)
    @PostMapping("/wxpub/configPlan/updateConfigPlanAccount")
    public Object updateConfigPlanAccount(@RequestBody ConfigPlanAccountUpdateDto configPlanAccountUpdateDto){
        configPlanService.updateConfigPlanAccount(configPlanAccountUpdateDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatSysPlugin, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改配置方案中分佣", notes = "修改配置方案中分佣")
    @ApiOperationSupport(order=9)
    @PostMapping("/wxpub/configPlan/updateConfigPlanProfit")
    public Object updateConfigPlanProfit(@RequestBody ConfigPlanProfitUpdateDto configPlanProfitUpdateDto){
        configPlanService.updateConfigPlanProfit(configPlanProfitUpdateDto);
        return true;
    }

    @ApiOperation(value = "查询淘宝联盟下拉列表", notes = "查询淘宝联盟下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=10)
    @PostMapping("/wxpub/configPlan/findTbTokenSelectList")
    public Object findTbTokenSelectList(@RequestBody TbTokenQuery tbTokenQuery){
        return configPlanService.findTbTokenSelectList(tbTokenQuery);
    }

    @ApiOperation(value = "查询饿了么下拉列表", notes = "查询饿了么下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=10)
    @PostMapping("/wxpub/configPlan/findElmTokenSelectList")
    public Object findElmTokenSelectList(){
        return configPlanService.findElmTokenSelectList(getLoginInfo().getMerId());
    }

//    @ApiOperation(value = "查询炮灰淘宝联盟下拉列表", notes = "查询炮灰淘宝联盟下拉列表", response = SelectIdVo.class)
//    @ApiOperationSupport(order=11)
//    @PostMapping("/wxpub/configPlan/findPhTbTokenSelectList")
//    public Object findPhTbTokenSelectList(){
//        return configPlanService.findPhTbTokenSelectList(getLoginInfo().getMerId());
//    }

    @ApiOperation(value = "查询唯品会联盟下拉列表", notes = "查询唯品会联盟下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=12)
    @PostMapping("/wxpub/configPlan/findVphTokenSelectList")
    public Object findVphTokenSelectList(){
        return configPlanService.findVphTokenSelectList(getLoginInfo().getMerId());
    }

//    @ApiOperation(value = "查询炮灰唯品会联盟下拉列表", notes = "查询炮灰唯品会联盟下拉列表", response = SelectIdVo.class)
//    @ApiOperationSupport(order=13)
//    @PostMapping("/wxpub/configPlan/findPhVphTokenSelectList")
//    public Object findPhVphTokenSelectList(){
//        return configPlanService.findPhVphTokenSelectList(getLoginInfo().getMerId());
//    }

    @ApiOperation(value = "查询京东联盟下拉列表", notes = "查询京东联盟下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=14)
    @PostMapping("/wxpub/configPlan/findJdTokenSelectList")
    public Object findJdTokenSelectList(){
        return configPlanService.findJdTokenSelectList(getLoginInfo().getMerId());
    }

    @ApiOperation(value = "查询多多进宝联盟下拉列表", notes = "查询多多进宝联盟下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=15)
    @PostMapping("/wxpub/configPlan/findPddTokenSelectList")
    public Object findPddTokenSelectList(){
        return configPlanService.findPddTokenSelectList(getLoginInfo().getMerId());
    }

    @ApiOperation(value = "查询推老板美团账号(共享)下拉列表", notes = "查询推老板美团账号(共享)下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=16)
    @PostMapping("/wxpub/configPlan/findTlbMtTokenSelectList")
    public Object findTlbMtTokenSelectList(){
        TlbTokenQuery tlbTokenQuery = new TlbTokenQuery();
        tlbTokenQuery.setMerId(getLoginInfo().getMerId());
        tlbTokenQuery.setType("mt");
        return configPlanService.findTlbTokenSelectList(tlbTokenQuery);
    }

    @ApiOperation(value = "查询抖音联盟下拉列表", notes = "查询抖音联盟下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=15)
    @PostMapping("/wxpub/configPlan/findDyTokenSelectList")
    public Object findDyTokenSelectList(){
        return configPlanService.findDyTokenSelectList(getLoginInfo().getMerId());
    }

    @ApiOperation(value = "查询推老板抖音账号(共享)下拉列表", notes = "查询推老板抖音账号(共享)下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=15)
    @PostMapping("/wxpub/configPlan/findTlbDyTokenSelectList")
    public Object findTlbDyTokenSelectList(){
        TlbTokenQuery tlbTokenQuery = new TlbTokenQuery();
        tlbTokenQuery.setMerId(getLoginInfo().getMerId());
        tlbTokenQuery.setType("dy");
        return configPlanService.findTlbTokenSelectList(tlbTokenQuery);
    }

    @ApiOperation(value = "查询推老板拼多多账号(共享)下拉列表", notes = "查询推老板拼多多账号(共享)下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=16)
    @PostMapping("/wxpub/configPlan/findTlbPddTokenSelectList")
    public Object findTlbPddTokenSelectList(){
        TlbTokenQuery tlbTokenQuery = new TlbTokenQuery();
        tlbTokenQuery.setMerId(getLoginInfo().getMerId());
        tlbTokenQuery.setType("pdd");
        return configPlanService.findTlbTokenSelectList(tlbTokenQuery);
    }

    @ApiOperation(value = "查询推广裂变奖励方案下拉列表框值", notes = "查询推广裂变奖励方案下拉列表框值", response = SelectIdVo.class)
    @ApiOperationSupport(order=17)
    @PostMapping("/wxpub/configPlan/findRegRewardRuleSelectList")
    public Object findRegRewardRuleSelectList(){
        return configPlanService.findRegRewardRuleSelectList(getLoginInfo().getMerId());
    }

    @ApiOperation(value = "查询系统平台域名下拉列表值", notes = "查询系统平台域名下拉列表值", response = SelectIdVo.class)
    @ApiOperationSupport(order = 18)
    @PostMapping("/wxpub/configPlan/findSysDomainSelectList")
    public Object findSysDomainSelectList(){
        //新建返回页面对象
        List<SelectIdVo> sysDomainVoList = new ArrayList<SelectIdVo>();
        //官方域名
        SelectIdVo selectIdVo_system = new SelectIdVo();
        selectIdVo_system.setKey(0L);
        selectIdVo_system.setValue("官方域名");
        sysDomainVoList.add(selectIdVo_system);

        //快站域名
        SysDomainQuery sysDomainQuery_kuaizhan = new SysDomainQuery();
        sysDomainQuery_kuaizhan.setMerId(getLoginInfo().getMerId());
        sysDomainQuery_kuaizhan.setStatus(1);
        sysDomainQuery_kuaizhan.setType(1);//快站域名
        List<SysDomain> sysDomainList_kuaizhan =  sysDomainMapper.findByList(sysDomainQuery_kuaizhan);
        if(sysDomainList_kuaizhan != null && sysDomainList_kuaizhan.size() > 0){
            for (SysDomain sysDomain : sysDomainList_kuaizhan) {
                SelectIdVo  SelectIdVo = new SelectIdVo();
                SelectIdVo.setKey(sysDomain.getId());
                SelectIdVo.setValue(sysDomain.getSiteDomain() + "（快站域名）");
                sysDomainVoList.add(SelectIdVo);
            }
        }

        //自有域名
        SysDomainQuery sysDomainQuery_private = new SysDomainQuery();
        sysDomainQuery_private.setMerId(getLoginInfo().getMerId());
        sysDomainQuery_private.setStatus(1);
        sysDomainQuery_private.setType(2);//自有域名
        List<SysDomain> sysDomainList_private =  sysDomainMapper.findByList(sysDomainQuery_private);
        if(sysDomainList_private != null && sysDomainList_private.size() > 0){
            for (SysDomain sysDomain : sysDomainList_private) {
                SelectIdVo  SelectIdVo = new SelectIdVo();
                SelectIdVo.setKey(sysDomain.getId());
                SelectIdVo.setValue(sysDomain.getSiteDomain() + "（自有域名）");
                sysDomainVoList.add(SelectIdVo);
            }
        }
        return sysDomainVoList;
    }


    @ApiOperation(value = "查询分佣方案下拉列表值", notes = "查询分佣方案下拉列表值", response = SelectIdVo.class)
    @ApiOperationSupport(order=19)
    @PostMapping("/wxpub/configPlan/findNormalRewardRuleSelectList")
    public Object findNormalRewardRuleSelectList(@RequestBody ConfigPlanNormalrewardruleDto configPlanNormalrewardruleDto){
        return configPlanService.findNormalRewardRuleSelectList(configPlanNormalrewardruleDto);
    }

    @ApiOperation(value = "查询会员类型组下拉列表", notes = "查询会员类型组下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 20)
    @PostMapping("/wxpub/configPlan/findUsertypeGroupSelect")
    public Object findUsertypeGroupSelect(){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        UserTypeGroupQuery userTypeGroupQuery = new UserTypeGroupQuery();
        userTypeGroupQuery.setMerId(getLoginInfo().getMerId());
        List<UserTypeGroup> userTypeGroupList = userTypeGroupMapper.findByList(userTypeGroupQuery);
        if(userTypeGroupList != null && userTypeGroupList.size() > 0){
            for(UserTypeGroup userTypeGroup : userTypeGroupList){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(userTypeGroup.getId()));
                selectKeyVo.setValue(userTypeGroup.getName());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatSysPlugin, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "设置默认配置方案", notes = "设置默认配置方案")
    @ApiOperationSupport(order=21)
    @PostMapping("/wxpub/configPlan/setDefault")
    public Object setDefault(@RequestBody ConfigPlanUpdateDto configPlanUpdateDto){
        configPlanService.setDefault(configPlanUpdateDto);
        return true;
    }

    @ApiOperation(value = "查询默认配置方案详情", notes = "查询默认配置方案详情", response = ConfigPlanVo.class)
    @ApiOperationSupport(order=22)
    @PostMapping("/wxpub/configPlan/findConfigPlanInfoIsDefault")
    public Object findConfigPlanInfoIsDefault(){
        Long merId = getLoginInfo().getMerId();
        String deviceType = DeviceTypeEnum.WX_PUB.getKey();
        return configPlanService.findConfigPlanInfoIsDefault(merId, deviceType);
    }

    @ApiOperation(value = "查询红包卡片模板列表", notes = "查询红包卡片模板列表", response = RedCardTempVo.class)
    @ApiOperationSupport(order=23)
    @PostMapping("/wxpub/configPlan/findRedCardTempSelectList")
    public Object findRedCardTempSelectList(){
        String redCardTemp_value = sysConfigMapper.selectByKey("redpackModel");
        List<RedCardTempVo> redCardTempVoList = JSONObject.parseArray(redCardTemp_value, RedCardTempVo.class);
        if(redCardTempVoList != null && redCardTempVoList.size() > 0){
            for (RedCardTempVo redCardTempVo : redCardTempVoList){
                redCardTempVo.setImg(uzaiConsoleSysConfig.getQiniuNetAddress() + redCardTempVo.getImg());
            }
        }
        return redCardTempVoList;
    }

    @SystemLogType(model = SystemModelEnum.DeviceWechatSysPlugin, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "批量修改插件信息", notes = "批量修改插件信息")
    @ApiOperationSupport(order=24)
    @PostMapping("/wxpub/configPlan/batchUpdateSysPlugin")
    public Object batchUpdateSysPlugin(@RequestBody ConfigPlanPluginBatchUpdateDto configPlanPluginBatchUpdateDto){
        configPlanService.batchUpdateSysPlugin(configPlanPluginBatchUpdateDto);
        return true;
    }


    @SystemLogType(model = SystemModelEnum.DeviceWechatSysPlugin, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "批量修改配置方案中的联盟", notes = "批量修改配置方案中的联盟")
    @ApiOperationSupport(order=25)
    @PostMapping("/wxpub/configPlan/batchUpdateConfigPlanAccount")
    public Object batchUpdateConfigPlanAccount(@RequestBody ConfigPlanAccountBatchUpdateDto configPlanAccountBatchUpdateDto){
        configPlanService.batchUpdateConfigPlanAccount(configPlanAccountBatchUpdateDto);
        return true;
    }

    @ApiOperation(value = "复制默认配置方案(初始化)", notes = "复制默认配置方案(初始化)", response = ConfigPlanVo.class)
    @ApiOperationSupport(order=22)
    @PostMapping("/wxpub/configPlan/copyDefaultConfig")
    public Object copyDefaultConfig(@RequestBody ConfigPlanCopyDefaultDto configPlanCopyDefaultDto){
        String deviceType = configPlanCopyDefaultDto.getDeviceType();
        if(StringUtils.isBlank(deviceType)){
            configPlanCopyDefaultDto.setDeviceType(DeviceTypeEnum.WX_PUB.getKey());
        }else if(!deviceType.equals(DeviceTypeEnum.DT_WORK_WECHAT.getKey())
                &&  !deviceType.equals(DeviceTypeEnum.WX_PUB.getKey())
        ){
            configPlanCopyDefaultDto.setDeviceType(DeviceTypeEnum.WX_PUB.getKey());
        }
        return configPlanService.copyDefaultConfig(configPlanCopyDefaultDto);
    }

}
